let uploadFile = require('@/utils/upload/uploadFile.js');
/**
 *  相册中上传 视频
 */
function uploadVideo() {
	return new Promise((resolve, reject) => {
		uni.chooseMedia({
			count: 1,
			mediaType: ['video'],
			sourceType: ['album'],
			success(res) {
				console.log(res.tempFiles[0])
				let tempFilePaths = res.tempFiles[0].tempFilePath;
				let time = new Date();
				let nowTime = uni.$u.timeFormat(time, 'yyyy-mm-dd');
				let nowTime2 = uni.$u.timeFormat(time, 'yyyy/mm/');
				uploadFile(
					tempFilePaths,
					'wxapp/video/' + nowTime2 + nowTime + '/',
					function(result) {
						console.log('======上传成功======')
						let video = {
							videoUrl: result,
							duration: res.tempFiles[0].duration
						}
						resolve(video)
					},
					function(result) {
						console.log('======上传失败======');
						reject(result)
					}
				);
			}
		})
	})
}

function uploadAudioFile() {
	return new Promise((resolve, reject) => {
		wx.chooseMessageFile({
			count: 1,
			type: 'file',
			success(res) {
				console.log('会话选择成功', res)
				let tempFilePaths = res.tempFiles[0].path;
				let time = new Date();
				let nowTime = uni.$u.timeFormat(time, 'yyyy-mm-dd');
				let nowTime2 = uni.$u.timeFormat(time, 'yyyy/mm/');
				uploadFile(
					tempFilePaths,
					'wxapp/video/' + nowTime2 + nowTime + '/',
					function(result) {
						console.log('======上传成功======')
						let data = {
							audioName: res.tempFiles[0].name,
							audioUrl: result,
							audioTime: uni.$u.timeFormat(time, 'yyyy-mm-dd hh:MM'),
						}
						resolve(data)
					},
					function(result) {
						console.log('======上传失败======');
						reject(result)
					}
				);
			},
			fail() {
				console.log('会话选择失败')
			}
		});
	})
}
export {
	uploadVideo,
	uploadAudioFile
}
